<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Admin\Renderable\FarmerImgs;
use App\Admin\Repositories\FarmerFeedback;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

class FarmerFeedbackController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FarmerFeedback(), function (Grid $grid) {
            $region_id = Admin::user()->region_id;
            if ($region_id) {
                $grid->model()->where('region_id', $region_id);
                $grid->model()->orderBy('id', 'desc');
            }
            $grid->column('id')->sortable();
            $grid->column('type')->display(function ($ems_type) {
                // 信件类别;意见建议1、申诉2、控告3、其他4
                switch ($ems_type) {
                    case 1:
                        return '遇到问题';
                        break;
                    default:
                        return '提供建议';
                }
            });
            $grid->column('content');
            $grid->column('imgs', '图片')
                ->display('查看')
                ->expand(function () {
                    return FarmerImgs::make(['imgs' => $this->imgs]);
                });

            $grid->column('status')->select([0 => '待处理', 1 => '处理中', 2 => '已处理']);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->disableEditButton(); // 禁用编辑按钮
            $grid->disableViewButton(); // 禁用详情按钮
            $grid->disableCreateButton(); // 禁用新增按钮
            $grid->quickSearch(['content'])->placeholder('搜索...');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->selectOne('type', '类型筛选', [1 => '遇到问题', 2 => '提供建议']);
                $selector->selectOne('status', '状态筛选', [0 => '待处理', 1 => '处理中', 2 => '已处理']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FarmerFeedback(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('content');
            $show->field('imgs')->image();
            $show->field('status');
            $show->field('region_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FarmerFeedback(), function (Form $form) {
            $form->display('id');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
